<?php

/**
 * mp actions.
 *
 * @package    explications_informatiques
 * @subpackage mp
 * @author     Guimezanes Cyrille
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class mpActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
	if(isset($request["choice"]))
	{
		
		if($request["choice"] == "0")
		{
			$type = "id_auteur";
			$suppr = "suppr_author";
		}
		else
		{
			$type = "id_destinataire";
			$suppr = "suppr_desti";
		}
	}
	else
	{
		$type = "id_destinataire";
		$suppr = "suppr_desti";
	}
	$this->forwardUnless($this->getUser()->isAuthenticated(),"headers","forbidden");
    $q = Doctrine_Query::create()
	->from('mp n')
	->leftJoin("n.SfGuardUserProfile aut")
	->leftJoin("aut.User")
	->leftJoin("n.SfGuardUserProfile_2 des")
	->leftJoin("des.User")
	->where('n.'.$type.' = ?', $this->getUser()->getGuardUser()->getId())
	->andWhere('n.'.$suppr.' = ?', "0")
	->andWhere('n.spam != ?', "1")
	->orderBy('n.date desc');
	$this->mps = $q->execute();
  }
  public function executeActions(sfWebRequest $request)
  {
	  if(isset($request["mess"]))
	  {
		  if(!is_array($request["mess"]))
		  	$request["mess"] = array($request["mess"]);
			
		  foreach($request["mess"] as $mess)
		  {
			  $q = Doctrine_Query::create()
				->from('mp')
				->where("idmp = ?",$mess);
			  $mp = $q->fetchOne();
			  switch($request["actionToDO"])
			  {
				  case "suppr":
					if($mp["id_destinataire"] == $this->getUser()->getGuardUser()->getId())
						$mp->setSupprAuthor("1");
					elseif($mp["id_auteur"] == $this->getUser()->getGuardUser()->getId())
						$mp->setSupprDesti("1");
					$mp->save();	
					if($mp["suppr_desti"] == "1" && $mp["suppr_desti"] == $mp["suppr_auteur"])
						$mp->delete();
						
				  break;
				  case "read":
					if($mp["id_destinataire"] == $this->getUser()->getGuardUser()->getId())
					{
						$mp->setLu("1");
						$mp->save();
					}
				  break;
				  case "unread":
					if($mp["id_destinataire"] == $this->getUser()->getGuardUser()->getId())
					{
						$mp->setLu("0");
						$mp->save();
					}
				  break;
				  case "spam":
					if($mp["id_destinataire"] == $this->getUser()->getGuardUser()->getId())
					{
						$mp->setSpam("1");
						$mp->save();
					}
				  break;
			  }
		  }
	  }
	 
	  $this->redirect("mp/index");
  }
  public function executeShow(sfWebRequest $request)
  {
    $this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("AJOUT_MP"),"headers","forbidden");
	$q = Doctrine_Query::create()
	->from('mp n')
	->leftJoin("n.SfGuardUserProfile aut")
	->leftJoin("aut.SfGuardUser")
	->leftJoin("n.SfGuardUserProfile_2 des")
	->leftJoin("des.SfGuardUser")
	->where('n.idmp = ?', $request->getParameter('id'));
	$this->mp = $q->fetchOne();
	
	if(!in_array($this->getUser()->getGuardUser()->getId(),array($this->mp["id_destinataire"],$this->mp["id_auteur"])))
		$this->forward404();
		
	if($this->mp["lu"] == "0" && $this->mp["id_destinataire"] == $this->getUser()->getGuardUser()->getId())
	{
		$this->mp["lu"] = "1";
		$this->mp->save();
	}
    $this->forward404Unless($this->mp);
  }

  public function executeNew(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("AJOUT_MP"),"headers","forbidden");
	
    $this->form = new MpForm();
	$this->form->setDefault('id_destinataire', $request->getParameter('dest'));
  }

  public function executeCreate(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("AJOUT_MP"),"headers","forbidden");
    $this->forwardUnless($request->isMethod(sfRequest::POST),"headers","forbidden");

    $this->form = new MpForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }



  public function executeDelete(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated(),"headers","forbidden");
    $this->forward404Unless($mp = Doctrine_Core::getTable('Mp')->find(array($request->getParameter('idmp'))), sprintf('Object mp does not exist (%s).', $request->getParameter('idmp')));
    if(!in_array($this->getUser()->getGuardUser()->getId(),array($mp["id_destinataire"],$mp["id_auteur"])))
		$this->forward404();
	if($this->getUser()->getGuardUser()->getId() == $mp["id_destinataire"])
		$mp["suppr_desti"] = 1;
	if ($this->getUser()->getGuardUser()->getId() == $mp["id_auteur"])
		$mp["suppr_author"] = 1;
	
	if($mp["suppr_desti"] == 1 && $mp["suppr_author"] == 1)
		$mp->delete();
	else
		$mp->save();
	
    $this->redirect('mp/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("AJOUT_MP"),"headers","forbidden");
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $mp = new Mp();
	  $mp->setIdAuteur($this->getUser()->getGuardUser()->getId());
	  $mp->setIdDestinataire($request["mps"]["id_destinataire"]);
	  $mp->setSujet($request["mps"]["sujet"]);
	  $mp->setMessage($request["mps"]["texte"]);
	  $mp->setDate(date("Ymdhis"));
	  $mp->save();
	   if ($cache = $this->getContext()->getViewCacheManager())
		  {	
		  	$cache->remove('@sf_cache_partial?module=headers&action=_newmp&sf_cache_key='.$request["mps"]["id_destinataire"]);
		  }
	  if($request->getParameter('action') == "create")
	  	$this->getUser()->setFlash("info","Votre message à été envoyé!");
	  else
	  	$this->getUser()->setFlash("info","Message modifié");
      $this->redirect('mp/show?id='.$mp->getIdmp());
   
	}
  }
}
